<?php
/**
 * @author Zwc
 * Created On 2021/6/21 20:27
 */

namespace app\auth\controller;


use app\common\controller\AdminBaseController;

class Upload extends AdminBaseController
{
    /**
     * 图片上传
     */
    public function upload() {
        $fileName = 'files';
        $file = $this->request->file('file');
        $savename = \think\facade\Filesystem::putFile($fileName, $file);
        $savename = str_replace('\\','/',$savename);
//        $filePath = $_SERVER['DOCUMENT_ROOT'] .'/upload/'.$savename;
//        $arr = explode('/',$savename);
//        $ossService = new OssService();
//        $res = $ossService->uploadFiles($filePath,$arr[count($arr) - 1]);
        return json([
            'code' => 0,
            'msg' => '上传成功',
            'src' => 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$savename
//            'src' => $res,
        ]);
    }

    /**
     * wang编辑器用到的图片上传
     */
    public function upload_wang() {
        $fileName = 'files';
        $file = $this->request->file('file');
        $savename = \think\facade\Filesystem::putFile($fileName, $file);
        if($savename){
            $savename = str_replace('\\','/',$savename);
//            $filePath = $_SERVER['DOCUMENT_ROOT'] .'/upload/'.$savename;
//            $arr = explode('/',$savename);
//            $ossService = new OssService();
//            $res = $ossService->uploadFiles($filePath,$arr[count($arr) - 1]);
//            $da = [
//                'errno' => 0,
//                'msg' => '上传成功',
//                'data' => [
//                    [
//                        'url' => $res
//                    ]
//                ]
//            ];
            return json([
                'errno' => 0,
                'msg' => '上传成功',
                'data' => [
                    [
                        'url' => 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$savename
                    ]
                ]
            ]);
        }else{
            echo $file->getError();
        }
    }
    // TODO 上传附件
    public function uploadFile() {
        $fileName = 'files';
        $file = $this->request->file('file');
        $orgName = $file->getOriginalName();
        $savename = \think\facade\Filesystem::putFile($fileName, $file);
        $savename = str_replace('\\','/',$savename);
        return json([
            'code' => 0,
            'msg' => '上传成功',
            'orgName' => $orgName,
            'src' => '/upload/'.$savename
        ]);
    }


}